<?php
	class ProfileHandler
	{
		// Membervariables
		private $_con = null;
		
		// Constants
		const FAILEDTOADDPROFILE = "Failed to add profile-object to the database reason unknown.";
		const BIRTHDAYCONTAINSHTML = "The birthday contained HTML check time and ip.";
		const LOCATIONCONTAINSHTML = "The location contained HTML check time and ip.";
		const LOCATIONMAXCHARLENGTH = 128;
		
		public function __construct(DBConnection $con)
		{
			$this->_con = $con;
		}
		
		// Generates a profile from user input.
		public function GenerateProfile($userid)
		{
			$profile = new Profile();
			$doUserAlreadyHaveAProfile = $this->GetProfile($userid);
			
			// profileid
			if ($doUserAlreadyHaveAProfile != null)
			{
				$profile->profileid = $doUserAlreadyHaveAProfile->profileid;
			}
			else 
			{	
				$profile->profileid = $this->GenerateProfileId();
			}
			// userid
			$profile->userid = $userid;
			// showgiven
			if (isset($_POST[ProfileView::SHOWGIVENNAME]) == true)
			{
				$profile->showgiven = $_POST[ProfileView::SHOWGIVENNAME];
			}
			else 
			{
				$profile->showgiven = 0;
			}
			//showsur
			if (isset($_POST[ProfileView::SHOWSURNAME]) == true)
			{
				$profile->showsur = $_POST[ProfileView::SHOWSURNAME];
			}
			else 
			{
				$profile->showsur = 0;
			}
			// showmail
			if (isset($_POST[ProfileView::SHOWEMAIL]) == true)
			{
				$profile->showmail = $_POST[ProfileView::SHOWEMAIL];
			}
			else 
			{
				$profile->showmail = 0;
			}
			// birthday
			$profile->birthday = $_POST[ProfileView::SHOWBIRTHDAY];
			// location
			$profile->location = $_POST[ProfileView::SHOWLOCATION];
			// gender
			$profile->gender = $_POST[ProfileView::SHOWGENDER];
			// personal text
			$profile->personaltext = $_POST[ProfileView::SHOWPERSONALTEXT];
			
			return $profile;
		}
		
		// Returns a profile-object.
		public function GetProfile($userid)
		{
			$classname = "Profile";
			$sql = "SELECT * FROM profiles WHERE userid = ?";
			
			$stmt = $this->_con->Prepare($sql);
			
			$stmt->bind_param("i", $userid);
			
			$arr = $this->_con->RunAndFetchObjects($stmt, $classname, "userid");

			if (count($arr) == 1)
			{
				return $arr[$userid];
			}
			else
			{
				return null;
			}	
		}
		
		// Returns a list of all the profiles in the database.
		public function GetProfileList()
		{
			$classname = "Profile";
			$sql = "SELECT * FROM profiles";
			
			$stmt = $this->_con->Prepare($sql);
			
			$arr = $this->_con->RunAndFetchObjects($stmt, $classname, "profileid");
			
			return $arr;
		}
		
		// Saves the profile to the database.
		public function SaveProfile(Profile $profile)
		{
			if ($this->GetProfile($profile->userid) != null)
			{
				$sql = "UPDATE `profiles` SET `profileid`=?,`userid`=?,`showgiven`=?,`showsur`=?,`showmail`=?,`birthday`=?,`location`=?,`gender`=?,`personaltext`=? WHERE `profileid`='$profile->profileid'";
				$stmt = $this->_con->Prepare($sql);
				
				$stmt->bind_param("iiiiissis", $profile->profileid, $profile->userid, $profile->showgiven, $profile->showsur, $profile->showmail, $profile->birthday, $profile->location, $profile->gender, $profile->personaltext);
				
				$stmt->execute();
				
				$stmt->close();
				
				Log::LogMessage("Changes was saved.");
			}
			else 
			{
				
				$sql = "INSERT INTO `profiles`(`profileid`, `userid`, `showgiven`, `showsur`, `showmail`, `birthday`, `location`, `gender`, `personaltext`)" .
				"VALUES (?,?,?,?,?,?,?,?,?)";
				
				$stmt = $this->_con->Prepare($sql);
				
				$stmt->bind_param("iiiiissis", $profile->profileid, $profile->userid, $profile->showgiven, $profile->showsur, $profile->showmail, $profile->birthday, $profile->location, $profile->gender, $profile->personaltext);
				
				$stmt->execute();
				
				$stmt->close();
				
				if ($this->GetProfile($profile->userid) == null)
				{
					Log::LogError("A database related error occured.", ProfileHandler::FAILEDTOADDPROFILE);
				}
			}
		}
		
		// Generates a new unique profileId.
		private function GenerateProfileId()
		{
			// Get a list of all the profiles.
			$profileList = $this->GetProfileList();
			$newId = 0;
			
			foreach ($profileList as $profile)
			{
				if ($newId == $profile->profileid)
				{
					$newId++;
				}
				else 
				{
					return $newId;
				}
			}
			
			return $newId;
		}
		
		// Returns true/false depending on if the profile passed the validation or not.
		public function ProfilePassedValidation(\Profile $profile)
		{
			$validation = new Validation();
			$comparisonProfile = $profile;
			
			// Check if the bithday contained html.
			$comparisonProfile->birthday = $validation->StripAll($comparisonProfile->birthday);
			
			if ($comparisonProfile->birthday != $profile->birthday)
			{
				Log::LogError("Invalid input.", $this::BIRTHDAYCONTAINSHTML);
				return false;
			}
			
			// Check if the location contained html.
			$comparisonProfile->location = $validation->StripAll($comparisonProfile->location);
			
			if ($comparisonProfile->location != $profile->location)
			{
				Log::LogError("Invalid input.", $this::LOCATIONCONTAINSHTML);
				return false;
			}
			
			// Check if the location contained html.
			if (strlen($profile->location) > $this::LOCATIONMAXCHARLENGTH)
			{
				Log::LogMessage("The location was to long, max number of characters allowed: " . $this::LOCATIONMAXCHARLENGTH . ".");
				return false;
			}
			
			return true;
		}	
	}